package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.bac.BacBloodCultrue;
import com.ysd.lis.entity.bac.BacPublisFirstReport;
import com.ysd.lis.mapper.bac.BacBloodCultrueMapper;
import com.ysd.lis.mapper.bac.BacPublisFirstReportMapper;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.bac.BacPublisFirstReportService;
import com.ysd.common.Result;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-08-27
 */
@Service
public class BacPublisFirstReportServiceImpl extends ServiceImpl<BacPublisFirstReportMapper, BacPublisFirstReport> implements BacPublisFirstReportService {

    @Autowired
    BacPublisFirstReportMapper bacPublisFirstReportMapper;
    @Autowired
    BacBloodCultrueMapper bacBloodCultrueMapper;
    @Autowired
    SysBasDictDetailService sysBasDictDetailService;

    @Override
    public Result getBacPublisFirstReports(BacPublisFirstReport bacPublisFirstReport) {
        LambdaQueryWrapper<BacPublisFirstReport> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BacPublisFirstReport::getDelFlag, 0);
        lambdaQueryWrapper.eq(BacPublisFirstReport::getRptGroup, bacPublisFirstReport.getRptGroup());
        lambdaQueryWrapper.eq(BacPublisFirstReport::getSampleNo, bacPublisFirstReport.getSampleNo());
        if (ToolsUtils.isNotEmpty(bacPublisFirstReport.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacPublisFirstReport.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            lambdaQueryWrapper.ge(BacPublisFirstReport::getSampleda, startTime);
            lambdaQueryWrapper.le(BacPublisFirstReport::getSampleda, endTime);
        }
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacPublisFirstReport.getTestFlowNo()), BacPublisFirstReport::getTestFlowNo, bacPublisFirstReport.getTestFlowNo());
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacPublisFirstReport.getTestNodeNo()), BacPublisFirstReport::getTestNodeNo, bacPublisFirstReport.getTestNodeNo());
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacPublisFirstReport.getCulCombinaNo()), BacPublisFirstReport::getCulCombinaNo, bacPublisFirstReport.getCulCombinaNo());
        lambdaQueryWrapper.eq(ToolsUtils.isNotEmpty(bacPublisFirstReport.getCulMediumNo()), BacPublisFirstReport::getCulMediumNo, bacPublisFirstReport.getCulMediumNo());
        if (ToolsUtils.isNotEmpty(bacPublisFirstReport.getSearchType()) && bacPublisFirstReport.getSearchType().equals(2)) {
            lambdaQueryWrapper.and(p -> p.ne(BacPublisFirstReport::getIsCancel, 1).or().isNull(BacPublisFirstReport::getIsCancel));
        }
        if (ToolsUtils.isNotEmpty(bacPublisFirstReport.getSearchType()) && bacPublisFirstReport.getSearchType().equals(3)) {
            lambdaQueryWrapper.eq(BacPublisFirstReport::getIsCancel, 1);
        }
        List<BacPublisFirstReport> list = bacPublisFirstReportMapper.selectList(lambdaQueryWrapper);

        if (ToolsUtils.isNotEmpty(list)) {
            List<String> ids = list.stream().map(BacPublisFirstReport::getBloodcultureId).collect(Collectors.toList());

            LambdaQueryWrapper<BacBloodCultrue> bacBloodCultrueQueryMapper = new LambdaQueryWrapper<>();
            bacBloodCultrueQueryMapper.eq(BacBloodCultrue::getDelFlag, 0);
            bacBloodCultrueQueryMapper.in(BacBloodCultrue::getId, ids);
            List<BacBloodCultrue> bacBloodCultrueList = bacBloodCultrueMapper.selectList(bacBloodCultrueQueryMapper);

            List<Map<String, Object>> samplingSiteList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("samplingSite").getData();
            List<Map<String, Object>> culbottleNoList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("incubator").getData();

            for (BacBloodCultrue bacBloodCultrue1 : bacBloodCultrueList) {
                if (org.apache.commons.lang3.StringUtils.isNotBlank(bacBloodCultrue1.getSamplingSite()) && org.apache.commons.collections4.CollectionUtils.isNotEmpty(samplingSiteList)) {
                    samplingSiteList.stream().filter(a -> a.get("code").equals(bacBloodCultrue1.getSamplingSite())).findFirst().ifPresent(stringObjectMap -> bacBloodCultrue1.setSamplingSiteNa((String) stringObjectMap.get("name")));
                }
                if (org.apache.commons.lang3.StringUtils.isNotBlank(bacBloodCultrue1.getInstrCode()) && org.apache.commons.collections4.CollectionUtils.isNotEmpty(culbottleNoList)) {
                    culbottleNoList.stream().filter(a -> a.get("code").equals(bacBloodCultrue1.getInstrCode())).findFirst().ifPresent(stringObjectMap -> bacBloodCultrue1.setInstrNa((String) stringObjectMap.get("name")));
                }
            }

            for (BacPublisFirstReport bacPublisFirstReport1 : list) {
                List<BacBloodCultrue> collect = bacBloodCultrueList.stream().filter(a -> a.getId().equals(bacPublisFirstReport1.getBloodcultureId())).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(collect)) {
                    BacBloodCultrue bacBloodCultrue = collect.get(0);
                    bacPublisFirstReport1.setBloodcultureDto(bacBloodCultrue);
                }
            }
        }
        return Result.succ(1, "成功", list);
    }

    @Override
    public Result cancelPublisFirstReport(BacPublisFirstReport bacPublisFirstReport) {
        if (ToolsUtils.isNotEmpty(bacPublisFirstReport)) {
            bacPublisFirstReport.setIsCancel(1);
            if (ToolsUtils.isEmpty(bacPublisFirstReport.getCancelDate())) {
                bacPublisFirstReport.setCancelDate(new Timestamp(System.currentTimeMillis()));
            }
            SysUser sysUser = null;
            if (ToolsUtils.isEmpty(bacPublisFirstReport.getCancelDoctor())) {
                try {
                    sysUser = RedisUserManager.getUser();
                    bacPublisFirstReport.setCancelDoctor(ToolsUtils.isNotEmpty(sysUser) && ToolsUtils.isNotEmpty(sysUser.getUsername()) ? sysUser.getUsername() : "");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (ToolsUtils.isEmpty(bacPublisFirstReport.getCancelReason())) {
                bacPublisFirstReport.setCancelReason(ToolsUtils.isNotEmpty(sysUser) && ToolsUtils.isNotEmpty(sysUser.getUsername()) ? "由" + sysUser.getRealname() + "取消 " : "");
            }
            int i = bacPublisFirstReportMapper.updateById(bacPublisFirstReport);
            return Result.succ(1, "成功", bacPublisFirstReport);
        }
        return Result.fail("取消失败");
    }

    @Override
    public Result deletePublisFirstReport(BacPublisFirstReport bacPublisFirstReport) {
        if (ToolsUtils.isEmpty(bacPublisFirstReport)) {
            return Result.fail("没有要删除的报告");
        }
        if (ToolsUtils.isNotEmpty(bacPublisFirstReport.getIsCancel()) && !bacPublisFirstReport.getIsCancel().equals(1)) {
            return Result.fail("请先取消发布报告");
        }
        int i = bacPublisFirstReportMapper.deleteById(bacPublisFirstReport.getId());
        return Result.succ(i > 0 ? 1 : 0, i > 0 ? "删除成功" : "删除失败", bacPublisFirstReport);
    }
}
